<template>
  <div class="deploy-dialog">
    <el-dialog
      title="查看详情"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="760px"
      @close="dialogHandle('cancel')"
    >
      <el-form
        v-loading="loading"
        label-position="right"
        ref="modelFormRef"
        label-width="100px"
        :model="modelForm"
      >
        <el-row>
          <el-col v-for="item in columns" :key="item.key" :span="12">
            <el-form-item :label="item.label" :prop="item.key">
              <el-input v-model="modelForm[item.key]" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="执行状态" prop="response">
          <el-input
            type="textarea"
            v-model="modelForm.response"
            disabled
            :rows="15"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogHandle('update')"
          >刷新</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { piplineGetExecWithRunDetail } from "@/api/pipeline/pipeline-execution";

export default {
  name: "deployDialog",
  props: {
    dialogType: {
      type: String,
      default: "add",
    },
    id: [String, Number],
  },
  data() {
    return {
      loading: false,
      visible: true,
      modelForm: {
        id: "",
        deployId: "",
        env: "",
        region: "",
        projectId: "",
        piplineId: "",
        piplineRunId: "",
        tag: "",
        batchId: "",
        createTime: "",
        response: "",
      },
      columns: [
        { label: "执行id", key: "id" },
        { label: "部署id", key: "deployId" },
        { label: "执行批次", key: "batchId" },
        { label: "流水线执行id", key: "piplineRunId" },
        { label: "执行人", key: "creator" },
        { label: "执行时间", key: "createTime" },
      ],
    };
  },
  mounted() {
    this.getInit();
  },
  methods: {
    async getInit() {
      this.loading = true;
      try {
        const { data } = await piplineGetExecWithRunDetail(this.id);
        this.modelForm = data;
        // 格式化执行状态数据
        if (this.modelForm.response) {
          this.modelForm.response = JSON.stringify(data.response, null, 2);
        }
      } catch (error) {
        this.$message.error(error);
      } finally {
        this.loading = false;
      }
    },
    dialogHandle(type) {
      switch (type) {
        case "cancel":
          this.$emit("dialogHandle", type);
          break;
        case "update":
          this.getInit();
          break;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.deploy-dialog {
  &-add-tag {
    height: 32px;
    line-height: 32px;
    padding: 0 12px;
  }

  .el-row .el-form-item {
    margin-bottom: 16px;
  }

  :deep(.el-input),
  :deep(.el-input__inner) {
    height: 32px;
    line-height: 32px;
  }
}
</style>
